package com.xfcycle.zebrabikegp.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;

import com.xfcycle.zebrabikegp.listener.CleanAppCacheListener;

import java.io.File;

/**
 * 获取App相关信息工具类
 * Created by xFcycle on 2017/6/1.
 */

public class AppInfoUtil {
    private static final String TAG = "AppInfoUtil";
    private CleanAppCacheListener cleanAppCacheListener;
    private String[] cacheUnit = new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB"};
    private Context context;

    public AppInfoUtil(Context context){
        this.context = context;
    }

    /**
     * 获取当前App版本号
     *
     * @param context 上下文信息
     * @return 返回的当前App版本号
     */
    public static String getAPPVersion(Context context) {
        String version = null;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = info.versionName;
            Log.e("===", "getVersion: " + info.versionName + " " + info.versionCode);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }

    /**
     * 获取当前app缓存数据大小 字符串
     *
     * @return 返回结果字符串
     */
    public String getAppCacheSize() {

        double cacheAmount = getAppCacheAmount();

        int unitTag = 0;

        while (cacheAmount > 1000) {
            cacheAmount = cacheAmount / 1024d;
            unitTag++;
        }

        String cacheResult = TransStringUtil.getDecimalPointTowPlaceStr(cacheAmount+"") + (unitTag > cacheUnit.length ? "" : cacheUnit[unitTag]);

        return cacheResult;
    }

    /**
     * 获取App当前缓存内容
     *
     * @return 返回的 内存大小 long 值
     */
    private long getAppCacheAmount() {

        long cacheSize = 0;

        cacheSize += null == context.getCacheDir() ? 0 : fileSize(context.getCacheDir());

        //判断手机是否有外挂存储介质，计算外存储介质中的缓存文件
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            cacheSize += null == context.getExternalCacheDir() ? 0 : fileSize(context.getExternalCacheDir());
        }

        return cacheSize;
    }

    /**
     * 计算当前文件所占空间
     *
     * @param file 所需要计算的文件
     * @return 计算的文件大小结果
     */
    private long fileSize(File file) {

        long size = 0;

        if (null == file) {
            return 0;
        }

        if (file.exists() && file.isDirectory()) {
            String[] childrenFileNames = file.list();
            for (int i = 0; i < childrenFileNames.length; i++) {
                File childrenFile = new File(file, childrenFileNames[i]);
                if (childrenFile.exists() && childrenFile.isFile()) {
                    size += childrenFile.length();
                }
                if (childrenFile.exists() && childrenFile.isDirectory()) {
                    size += fileSize(childrenFile);
                }
            }
        }

        return size;
    }

    /**
     * 清楚App中的缓存数据
     */
    public void cleanCache() {

        deleteFile(context.getCacheDir());
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            deleteFile(context.getExternalCacheDir());
        }

        cleanAppCacheListener.cleanAppCacheFinish();
    }

    /**
     * 自定义方法，删除文件
     *
     * @param file 所需要删除的文件
     */
    private void deleteFile(File file) {

        if (null == file) {
            return;
        }

        if (file.exists() && file.isDirectory()) {
            String[] childrenFileNames = file.list();
            for (int i = 0; i < childrenFileNames.length; i++) {
                File childrenFile = new File(file, childrenFileNames[i]);
                if (childrenFile.exists() && childrenFile.isFile()) {
                    childrenFile.delete();
                }
                if (childrenFile.exists() && childrenFile.isDirectory()) {
                    deleteFile(childrenFile);
                }
            }
        }
    }

    public void setOnCleanAppCacheListener(CleanAppCacheListener cleanAppCacheListener) {
        this.cleanAppCacheListener = cleanAppCacheListener;
    }
}
